<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="filters.keyword" placeholder="关键字"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="search">查询</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表
       @selection-change="selsChange" 选的不一样就会触发

    -->
    <el-table :data="productOrders" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="orderSn" label="订单编号" width="120" sortable>
      </el-table-column>
      <el-table-column prop="product.name" label="标题" width="150" sortable>
      </el-table-column>
      <el-table-column prop="state" label="订单状态" width="100" sortable>
        <template slot-scope="props">
							  <span v-if="props.row.state ==3">
								<span style="color:#00B46D">已接单</span>
							  </span>
          <span v-else>
								<span style="color:#D75C89">驳回</span>
                 			 </span>
        </template>
      </el-table-column>
      <el-table-column prop="price" label="价格" width="80" sortable>
      </el-table-column>
      <!--private Employee manager;//管理员-->
      <!--private SearchMasterMsg parent;//上级部门-->

      <el-table-column prop="user.username" label="下单用户" width="120" sortable>
      </el-table-column>
      <el-table-column prop="digest" label="备注"  sortable><!--width="250"-->
      </el-table-column>
      <!--<el-table-column prop="digest" label="摘要" width="160" sortable>-->
      <!--</el-table-column>-->
<!--      <el-table-column prop="address" label="地址" width="200" sortable>-->
<!--      </el-table-column>-->
      <el-table-column label="操作">
        <template scope="scope">
          <el-button size="small" @click="confirmOrder(scope.$index, scope.row)">订单确认</el-button>
          <el-button type="danger" size="small" @click="handleCancel(scope.$index, scope.row)">取消</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <!--如果没有选中就不能使用:disabled="this.sels.length===0"-->
      <el-button type="danger" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination layout="prev, pager, next" :current-page="page" @current-change="handleCurrentChange" :page-size="pageSize" :total="total" style="float:right;">
      </el-pagination>
    </el-col>

    <!--新增或编辑界面-->
    <el-dialog title="确认订单" :visible.sync="productOrderFormVisible" :close-on-click-modal="false">
      <!--:model="productOrderForm 模型绑定-->
      <!--:rules="productOrderFormRules" 校验规则-->
      <!--ref="productOrderForm" 给该表单定义一个引用名-->
      <el-form :model="productOrderForm" label-width="80px" :rules="productOrderFormRules" ref="productOrderForm">
        <el-form-item label="支付方式" prop="payType">
          <el-select v-model="productOrderForm.payType" placeholder="请选择">
            <el-option
                v-for="item in payTypeOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="支付金额" prop="money">
          <el-input v-model="productOrderForm.money" style="width: 200px" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="productOrderFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="confirmOrder" :loading="productOrderLoading">确认</el-button>
      </div>
    </el-dialog>

  </section>
</template>

<script>
export default {
  data() {
    return {
      filters: {
        keyword: ''
      },
      productOrders: [],
      total: 0,
      page: 1,
      pageSize:10,
      listLoading: false,
      sels: [],//列表选中列

      productOrderFormVisible: false,//新增界面是否显示
      productOrderLoading: false,
      productOrderFormRules: {
      },
      productOrderForm: {
        id:null,
        payType: '',
        money: ''
      },
      //支持三种选项
      payTypeOptions: [{
        value: 0,
        label: '余额'
      }, {
        value: 1,
        label: '垫付'
      }, {
        value: 2,
        label: '银行转账'
      }],
    }
  },
  methods: {
    search() {
      //查询完，原来分页条件已经没用了，要跑到第一页去
      this.page = 1;
      this.getAcceptProductOrders();
    }
    ,
    handleCurrentChange(val) {
      this.page = val;
      this.getAcceptProductOrders();
    },
    //获取收购订单列表
    getAcceptProductOrders() {
      let para = {
        currentPage: this.page,
        pageSize:this.pageSize,
        keyword: this.filters.keyword
      };
      this.listLoading = true;
      this.$http.post("/orderProduct/acceptlist",para)
          .then(result=>{
            result = result.data; //取出data这一层
            this.total = result.totals;
            this.productOrders = result.rows;
            this.listLoading = false;
          })
          .catch(result=>{
            alert("系统错误！")
          })
    },
    //显示编辑界面
    handleConfirm: function (index, row) {
      //重置
      this.productOrderForm={
        id:null,
        payType: '',
        money: ''
      }
      //设置id
      this.productOrderForm.id = row.id;
      this.productOrderFormVisible = true; // 显示编辑框
    },
    //保存
    confirmOrder: function (index, row) {
      // 表单上定义了ref="productOrderForm"
          this.$confirm('确认订单吗？', '温馨提示', {}).then(() => {
            this.productOrderLoading = true;
            //拷贝一份新数据
            // let para = Object.assign({}, this.productOrderForm);

            this.$http.get("/orderProduct/confirm/"+row.id)
                .then(result => {
                  result = result.data
                  //去除忙等框
                  this.productOrderLoading = false;
                  //做提示
                  if(result.success){
                    this.$message({
                      message: '操作成功',
                      type: 'success'
                    });
                  }else{
                    this.$message({
                      message: result.message,
                      type: 'error'
                    });
                  }
                  //关闭对话框
                  this.productOrderFormVisible =false;
                  //重新查询数据
                  this.getAcceptProductOrders();
                })
                .catch(result=>{
                  alert("系统错误！");
                  this.productOrderLoading = false;
                })
            ;
          });
        }
      ,
    //所有选中对象都会传进来
    selsChange: function (sels) {
      this.sels = sels;
      console.log(this.sels)
    },
    //删除
    handleCancel: function (index, row) {
      this.$confirm('确认取消该订单吗?', '温馨提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        let url = "/orderProduct/cancel/"+row.id;
        this.$http.get(url)
            .then(result=>{
                  result = result.data; //{sucess:false,message}
                  //去除忙等框
                  this.listLoading = false;

                  if(result.success){
                    this.page = 1;
                    //提示
                    this.$message({
                      message: '取消成功',
                      type: 'success'
                    });
                    //重新查询数据
                    this.getAcceptProductOrders();
                  }else{
                    //提示
                    this.$message({
                      message: result.message,
                      type: 'error'
                    });
                    //重新查询数据
                    this.getAcceptProductOrders();
                  }
                }

            )
            .catch(result=>{
              //去除忙等框
              this.listLoading = false;
              //提示
              this.$message({
                message: '系统错误！',
                type: 'error'
              });
            })

      }).catch(() => {

      });
    }
  },
  mounted() {
    this.getAcceptProductOrders();
  }
}

</script>

<style scoped>

</style>